class cross:
    def __init__(self):
        self.fx=None
        self.timekey=None
        self.his=[]
    def oncalc(self,fast,slow,timekey=None):
        if fast is not None and slow is not None:
            if timekey   is not None:
                if timekey==self.timekey:
                    self.his.pop()
                    self.fx=self.his[-1]
            if self.fx is not None:
                if self.fx >= 1:
                    if fast < slow:  # 死叉
                        self.fx = -2
                    else:
                        self.fx = 1
                elif self.fx<=-1:
                    if fast > slow:  # 金叉
                        self.fx = 2
                    else:
                        self.fx = -1
            else:
                if fast > slow:
                    self.fx = 1
                elif fast < slow:
                    self.fx = -1
            if timekey   is not None:
                self.timekey=timekey
                self.his.append(self.fx)
                while len(self.his)>2:
                    self.his.pop(0)
        return self.fx

"""
金叉：2，死叉：-2  向上：1  向下：-1
支持重复计算，但是要指定timekey，只能向前重复计算
"""
class cross_v3:
    def __init__(self,isreverse=False):
        self.timekey=None
        self.his=[]
        if isreverse:
            self.oncalc=self.oncalc_True
        else:
            self.oncalc = self.oncalc_False
    def oncalc_False(self,c,a,b,timekey=None):
        rst=0
        if c is not None and a is not None and b is not None:
            if timekey   is not None:
                if timekey==self.timekey:
                    self.his.pop()
            if self.his:
                if c>a:
                    if self.his[-1][0]<=self.his[-1][1]:
                        rst=2
                elif c<b:
                    if self.his[-1][0]>=self.his[-1][2]:
                        rst=-2
            self.timekey=timekey
            self.his.append((c,a,b))
            while len(self.his)>2:
                self.his.pop(0)
        return rst
    def oncalc_True(self,c,a,b,timekey=None):
        rst=0
        if c is not None and a is not None and b is not None:
            if timekey   is not None:
                if timekey==self.timekey:
                    self.his.pop()
            if self.his:
                if self.his[-1][0]>=self.his[-1][1]:
                    if c<a:
                        rst=-2
                elif self.his[-1][0]<=self.his[-1][2]:
                    if c>b:
                        rst=2
            self.timekey=timekey
            self.his.append((c,a,b))
            while len(self.his)>2:
                self.his.pop(0)
        return rst


class cross_bybar:
    def __init__(self,line,mode="HLC"):
        self.fx = None
        self.timekey = None
        self.his = []
        self.line = line
        self.mode = mode  # "HLC","HL","OC"
        self.f={"HLC":self.oncalc_HLC, "HL":self.oncalc_HL, "OC":self.oncalc_OC}
        self.oncalc=self.f[mode]

    def oncalc_HL(self,b,timekey=None):
        if b:
            slow=b.get(self.line,None)
            if slow   is not None:
                if timekey   is not None:
                    if timekey == self.timekey:
                        self.his.pop()
                        self.fx = self.his[-1]
                if self.fx   is not None:
                    if self.fx >= 1:
                        if b["low"] < slow:  # 死叉
                            self.fx = -2
                        else:
                            self.fx = 1
                    elif self.fx <= -1:
                        if b["high"] > slow:  # 金叉
                            self.fx = 2
                        else:
                            self.fx = -1
                else:
                    if b["low"] >= slow and b["high"]>=slow:
                        self.fx = 1
                    elif b["low"] <= slow and b["high"]<=slow:
                        self.fx = -1
                if timekey   is not None:
                    self.timekey = timekey
                    self.his.append(self.fx)
                    while len(self.his) > 2:
                        self.his.pop(0)
        return self.fx
    def oncalc_HLC(self,b,timekey=None):
        if b:
            slow=b.get(self.line,None)
            if slow   is not None:
                if timekey   is not None:
                    if timekey == self.timekey:
                        self.his.pop()
                        self.fx = self.his[-1]
                if self.fx   is not None:
                    if self.fx >= 1:
                        if b["low"] < slow and b["close"]<slow:  # 死叉
                            self.fx = -2
                        else:
                            self.fx = 1
                    elif self.fx <= -1:
                        if b["high"] > slow and b["close"]>slow:  # 金叉
                            self.fx = 2
                        else:
                            self.fx = -1
                else:
                    if b["low"] >= slow and b["high"]>=slow and b["close"]>slow:
                        self.fx = 1
                    elif b["low"] <= slow and b["high"]<=slow and b["close"]<slow:
                        self.fx = -1
                if timekey   is not None:
                    self.timekey = timekey
                    self.his.append(self.fx)
                    while len(self.his) > 2:
                        self.his.pop(0)
        return self.fx
    def oncalc_OC(self,b,timekey=None):
        if b:
            slow=b.get(self.line,None)
            if slow   is not None:
                if timekey   is not None:
                    if timekey == self.timekey:
                        self.his.pop()
                        self.fx = self.his[-1]
                if self.fx   is not None:
                    if self.fx >= 1:
                        if b["close"] < slow:  # 死叉
                            self.fx = -2
                        else:
                            self.fx = 1
                    elif self.fx <= -1:
                        if b["close"]>slow:  # 金叉
                            self.fx = 2
                        else:
                            self.fx = -1
                else:
                    if b["open"]>=slow and b["close"]>slow:
                        self.fx = 1
                    elif b["open"]<=slow and b["close"]<slow:
                        self.fx = -1
                if timekey   is not None:
                    self.timekey = timekey
                    self.his.append(self.fx)
                    while len(self.his) > 2:
                        self.his.pop(0)
        return self.fx


"""
金叉：2，死叉：-2  向上：1  向下：-1
支持重复计算，但是要指定timekey，只能向前重复计算
"""
